﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace BoxApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Map.ReadMapFile();
            InstantiateGrid();
        }

        private void InstantiateGrid()
        {
            GameGrid.Children.Clear();
            for (int i = 0; i < Map.MaxCol; i++)
            {
                GameGrid.ColumnDefinitions.Add(new ColumnDefinition());
            }
            for (int i = 0; i < Map.MaxRow; i++)
            {
                GameGrid.RowDefinitions.Add(new RowDefinition());
            }
            for (int i = 0; i < Map.MaxRow; i++)
            {
                for (int j = 0; j < Map.MaxCol; j++)
                {
                    Grid.SetRow(Map.GridMatrix[i,j].Sprite, i);
                    Grid.SetColumn(Map.GridMatrix[i, j].Sprite, j);
                    GameGrid.Children.Add(Map.GridMatrix[i, j].Sprite);
                    Map.GridMatrix[i, j].Sprite.MouseLeftButtonDown += SpriteOnMouseLeftButtonDown;
                   
                }
            }
            PlacePlayer();
        }
        
        // code behind-ból is lehet eventre feliratkozni és bármi click eventjét lekezelhetjük
        private void SpriteOnMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            
        }

        private void KeyPress(object sender, KeyEventArgs e)
        {
            Map.KeyPressed(e);
            RedrawMap();
            PlacePlayer();
            if (Map.DidWeWin())
            {
                MessageBox.Show("nyertél");
                Map.CurrentLevel++;
                MainWindow main = new MainWindow();
                App.Current.MainWindow = main;
                this.Close();
                main.Show();
            }
        }

        private void PlacePlayer()
        {
            GameGrid.Children.Remove(Map.GridMatrix[Map.PlayerRow, Map.PlayerCol].Sprite);
            Image sprite = null;
            var bitmap = new BitmapImage(new Uri("pack://application:,,,/Resources/Sprites/player.jpg"));
            sprite = new Image();
            sprite.Source = bitmap;
            Grid.SetRow(sprite, Map.PlayerRow);
            Grid.SetColumn(sprite, Map.PlayerCol);
            GameGrid.Children.Add(sprite);
        }

        private void RedrawMap()
        {
            GameGrid.Children.Clear();
            for (int i = 0; i < Map.MaxRow; i++)
            {
                for (int j = 0; j < Map.MaxCol; j++)
                {
                    Grid.SetRow(Map.GridMatrix[i, j].Sprite, i);
                    Grid.SetColumn(Map.GridMatrix[i, j].Sprite, j);
                    GameGrid.Children.Add(Map.GridMatrix[i, j].Sprite);
                }
            }
        }
    
      

        private void menuReset_Click(object sender, RoutedEventArgs e)
        {
            MainWindow main = new MainWindow();
            App.Current.MainWindow = main;
            this.Close();
            main.Show();
        }

        private void menuExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
